package com.wust.weibo;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.wust.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;



public class WeiBoDetailActivity extends Activity {

	
    private ListView talkView;
   	private ArrayList<JSONObject> list ;
   	private BaseAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wei_bo_detail);
        talkView = (ListView) findViewById(R.id.list_detail);
		list = new ArrayList<JSONObject>();
        
		 Intent intent=getIntent();
	   
       String json=intent.getStringExtra("weiboid");
        
        /*********************************************************************************************************/
//     String   json="{\"headicon\":\"http://imgsrc.baidu.com/forum/abpic/item/08e4ebfdfc039245cf2c16bb8794a4c27c1e2597.jpg\","+
//  " \"user_name\":\"天之痕\","+
//  " \"user_id\":1,"+
//"   \"mess_id\":123,"+
//  " \"mess_content\":\"早上好啊\","+
// "  \"mess_time\":\"2012-07-17\","+
//  " \"mess_img_url\":null,"+
//"	\"comment\":[{\"comment_content\":\"好你妹\",\"comment_time\":\"2012-07-18\",\"user_nickname\":\"坑爹的人\"},"+
//	"{\"comment_content\":\"好a\",\"comment_time\":\"2012-07-18\",\"user_nickname\":\"坑爹的人的爹\"}]   "+
//" }";
        /**********************************************************************************************************/
        System.out.println("weiboid="+json);
        try {
			JSONObject obj=new JSONObject(json);
			list.add(obj);
			String comm=obj.getString("comment");
			System.out.println("****************************     in     WeiBodetail                                       *****************************************");
			
			JSONArray array=new JSONArray(comm);
			if(array!=null&&array.length()!=0)
			{
				for(int i=0;i<array.length();++i)
				{
					System.out.println("weibodetail : "+array.getJSONObject(i).toString());
					list.add(array.getJSONObject(i));
					System.out.println("list["+i+"]="+list.get(i));
				}
			}			
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			System.out.print("jso★★★★★★★●●●●●●●●●n error :"+e);
		}   	
		
        adapter=new DetailAdapter();
		talkView.setAdapter(adapter );
		
 }
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		System.out.println("****************************     end    WeiBodetail                             *****************************************");
	}
   
	
   

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.wei_bo_detail, menu);
//        return true;
//    }

	class ViewHolder
	{
		
		public TextView name;
		public TextView time;
		public TextView reply;
		public TextView mess;
		
	}
	class DetailAdapter  extends BaseAdapter
	{

		LayoutInflater flater =(LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		

		@Override
		public View getView(int position, View vvv, ViewGroup parent) {
			// TODO Auto-generated method stub
		
			
			String namestr = "name";
			String timestr="time";
			String constr="content";
			String replystr="reply";
			ViewHolder holder;
				holder=new ViewHolder();
				JSONObject obj=list.get(position);
				View v;
				System.out.println("getView ***********************:["+position+"]"+"="+obj.toString());
				if(position==0)
			{
						v=flater.inflate(R.layout.list_say_me_item, null);
						holder.name=(TextView) v.findViewById(R.id.messagegedetail_rov_icon);
						holder.time=(TextView) v.findViewById(R.id.messagedetail_row_name);
						holder.reply=(TextView)v.findViewById(R.id.messagedetail_row_date);
						holder.mess=(TextView) v.findViewById(R.id.messagedetail_row_text);
				
						try {
								namestr=obj.getString("user_name");
								timestr=obj.optString("mess_time");
								constr=obj.getString("mess_content");
								replystr="回复（"+(list.size()-1)+"）";
					
						} catch (JSONException e) {
					// TODO Auto-generated catch block
									e.printStackTrace();
							}					
				
						holder.name.setText(namestr);
						holder.time.setText(timestr);
						holder.reply.setText(constr);
						holder.mess.setText(replystr);
					return v;
						
					}
					else
					{
						v=flater.inflate(R.layout.list_say_he_item, null);
						namestr=obj.optString("user_nickname");
						timestr=obj.optString("comment_time");
						constr=obj.optString("comment_content");
						replystr=String.valueOf(position+1);
						
						holder.name=(TextView) v.findViewById(R.id.messagegedetail_rov_icon);
						holder.time=(TextView) v.findViewById(R.id.messagedetail_row_name);
						holder.reply=(TextView)v.findViewById(R.id.messagedetail_row_date);
						holder.mess=(TextView) v.findViewById(R.id.messagedetail_row_text);
						
						holder.name.setText(namestr);
						holder.time.setText(timestr);
						holder.reply.setText(constr);
						holder.mess.setText(replystr);
					
						return v;
						}
//			
		
		
		}
	}
	

    
}
